package com.yuncheng.spcyApi.spcy;

import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.busService.InitWjjlService;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.spcyApi.spcyService.XpryTimedTasksService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.IpRateLimiterUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.sqsx.YbmHcyListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author hua
 */
@Api(tags = "初始化申请事项文件-接口")
@RestController
@RequestMapping("/api/spzx-spcy/initWj")
public class InitSqsxWjjlController {

    private static final Logger log = LoggerFactory.getLogger(InitSqsxWjjlController.class);

    @Resource
    @Lazy
    private InitWjjlService initWjjlService;

    @Resource
    private XpryTimedTasksService xpryTimedTasksService;

    @Log(title = "中心-获取制作中补齐补正通知文件")
    @ApiOperation(value = "中心-获取制作中补齐补正通知文件")
    @GetMapping(value = "getZzzBqbzFile")
    public HttpResult getZzzBqbzFile(@RequestParam(value = "sqsxid",defaultValue = "0")String sqsxid){

        return HttpResult.ok(initWjjlService.getBqbzFileListByFwjlx2(sqsxid));
    }

    @Log(title = "企业端-获取补齐补正通知文件")
    @ApiOperation(value = "企业端-获取补齐补正通知文件")
    @GetMapping(value = "getBztzFileByQy")
    public HttpResult getBztzFileByQy(@RequestParam(value = "sqsxid",defaultValue = "0")String sqsxid){

        return HttpResult.ok(initWjjlService.getBztzFileByQy(sqsxid));
    }

//    @Log(title = "获取历史补齐补正文件列表")
//    @ApiOperation(value = "获取历史补齐补正文件列表")
    @GetMapping(value = "getHistoryBqbzFile")
    public HttpResult getHistoryBqbzFile(@RequestParam(value = "sqsxid",defaultValue = "0")String sqsxid){

        return HttpResult.ok(initWjjlService.queryHistoryWjjlList(sqsxid));
    }

    @Log(title = "获取制作中企业撤件通知文件")
    @ApiOperation(value = "获取制作中企业撤件通知文件")
    @GetMapping(value = "getZzzQycjFile")
    public HttpResult getZzzQycjFile(@RequestParam(value = "sqsxid",defaultValue = "0")String sqsxid){

        return HttpResult.ok(initWjjlService.getQycjFileListByFwjlx2(sqsxid));
    }

    @Log(title = "初始化制作中企业撤件通知文件-同步文档")
    @ApiOperation(value = "初始化制作中企业撤件通知文件-同步文档")
    @PostMapping(value = "initQycjFile")
    public HttpResult initQycjFile(@RequestBody ParamsBo paramsBo){

        initWjjlService.initQycjWjmbInYpsc(paramsBo.getSqsxid(), paramsBo.getOpinion());

        return HttpResult.ok("保存成功");
    }

    @Log(title = "发起缺陷调整通知书")
    @ApiOperation(value = "发起缺陷调整通知书")
    @GetMapping(value = "fqqxtz")
    public HttpResult fqqxtz(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }
        return initWjjlService.fqqxtzFile(sqsxid);
    }

    @Log(title = "发起缺陷调整通知书-完成")
    @ApiOperation(value = "发起缺陷调整通知书-完成")
    @GetMapping(value = "fqqxtzWc")
    public HttpResult fqqxtzWc(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        return initWjjlService.fqqxtzWc(sqsxid);
    }

    @Log(title = "初始化制作中补齐补正通知文件-同步文档")
    @ApiOperation(value = "初始化制作中补齐补正通知文件-同步文档")
    @PostMapping(value = "initZzzBqbzFile")
    public HttpResult initZzzBqbzFile(@RequestBody ParamsBo paramsBo){

//        initWjjlService.initBqbzTzWjmbInYpsc(paramsBo.getSqsxid(), paramsBo.getOpinion());

        return HttpResult.ok("保存成功");
    }

    @Log(title = "上传同步事项文件")
    @ApiOperation(value = "上传同步事项文件")
    @PostMapping(value = "uploadQxtztzs")
    public HttpResult uploadQxtztzs(@RequestPart("file") MultipartFile[] uploadFile,
                                  @RequestParam(value = "sqsxid",defaultValue = "") String sqsxid,
                                  @RequestParam(value = "type",defaultValue = "") String type
    ){
        if (uploadFile == null || uploadFile.length == 0) {
            return HttpResult.error("上传失败,请先选择文件！");
        }

        if (StringUtils.isBlank(sqsxid) || StringUtils.isBlank(type)){
            return HttpResult.error("参数为空！");
        }

        initWjjlService.initUploadZgtzs(uploadFile, sqsxid, type);

        return HttpResult.ok("上传成功");
    }

//    @Log(title = "获取综合评定报告文件")
//    @ApiOperation(value = "获取综合评定报告文件")
    @GetMapping(value = "getZhpdbgFile")
    public HttpResult getZhpdbgFile(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        List<SpcySqsxWjjl> wjjlList = initWjjlService.getZhpdbgFile(sqsxid);

        return HttpResult.ok(wjjlList);
    }

    /** 废弃 */
    @Log(title = "获取现场检查笔录附件")
    @GetMapping(value = "getXcjcblFile")
    public HttpResult oldblfj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return HttpResult.ok();
    }

    @Log(title = "方案制定-完成抽选人员-生成通知函")
    @ApiOperation(value = "方案制定-完成抽选人员-生成通知函")
    @PostMapping(value = "wccxrySctzh")
    public HttpResult wccxrySctzh(@RequestBody YbmHcyListVo bean){
        // 接口限流
        if (!IpRateLimiterUtils.tryAcquire(GetCurrentUserUtils.getCurrentUserId())){
            return HttpResult.error("访问过于频繁，请稍候再试！");
        }

        if (StringUtils.isBlank(bean.getSqsxid())){
            return HttpResult.error("参数为空！");
        }

//        initWjjlService.wccxrySctzh(bean.getSqsxid()); // 初始化文件记录

        xpryTimedTasksService.tjEndZxzAutoXpryRwList(bean.getSqsxid()); // 结束自动选派

        // 清空待确认记录
        xpryTimedTasksService.updateDqrXpjlToWcl(bean.getSqsxid(), bean.getQkyy());

        /*if (StringUtils.isBlank(bean.getFsmszt()) || bean.getFsmszt().equals(SpcyConstant.XPJL_SMSZT_NO)){
            // 不处理
        }else {*/
        xpryTimedTasksService.sendJctzSmsByTyxpry(bean.getSqsxid(), bean.getFdqrdxnr());

        return HttpResult.ok("操作成功");
    }


    @Log(title = "获取现场检查-检查情况专述-完成状态")
    @ApiOperation(value = "获取现场检查-检查情况专述-完成状态(true: 完成; false: 未完成)")
    @GetMapping(value = "getJcqkzsState")
    public HttpResult getjcqkzsState(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.verifyXcjcJcqkzsCompleteState(sqsxid);
    }

    @Log(title = "现场检查-检查情况专述-点击完成")
    @ApiOperation(value = "现场检查-检查情况专述-点击完成")
    @GetMapping(value = "getJcqkzsDjwc")
    public HttpResult getJcqkzsDjwc(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateXcjcJcqkzsCompleteState(sqsxid, true);
    }

    @Log(title = "现场检查-检查情况专述-点击重新编辑")
    @ApiOperation(value = "现场检查-检查情况专述-点击重新编辑")
    @GetMapping(value = "getJcqkzsCxbj")
    public HttpResult getJcqkzsCxbj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateXcjcJcqkzsCompleteState(sqsxid, false);
    }

    @Log(title = "获取现场检查-发现问题-完成状态")
    @ApiOperation(value = "获取现场检查-发现问题-完成状态(true: 完成; false: 未完成)")
    @GetMapping(value = "getFxwtState")
    public HttpResult getFxwtState(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.verifyXcjcFxwtCompleteState(sqsxid);
    }

    @Log(title = "现场检查-发现问题-点击完成")
    @ApiOperation(value = "现场检查-发现问题-点击完成")
    @GetMapping(value = "getFxwtDjwc")
    public HttpResult getFxwtDjwc(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateXcjcFxwtCompleteState(sqsxid, true);
    }

    @Log(title = "现场检查-发现问题-点击重新编辑")
    @ApiOperation(value = "现场检查-发现问题-点击重新编辑")
    @GetMapping(value = "getFxwtCxbj")
    public HttpResult getFxwtCxbj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateXcjcFxwtCompleteState(sqsxid, false);
    }

    @Log(title = "获取现场检查-检查结论-完成状态")
    @ApiOperation(value = "获取现场检查-检查结论-完成状态(true: 完成; false: 未完成)")
    @GetMapping(value = "getJcjlState")
    public HttpResult getJcjlState(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.verifyXcjcCyjlCompleteState(sqsxid);
    }

    @Log(title = "现场检查-检查结论-点击完成")
    @ApiOperation(value = "现场检查-检查结论-点击完成")
    @GetMapping(value = "getJcjlDjwc")
    public HttpResult getJcjlDjwc(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateXcjcCyjlCompleteState(sqsxid, true);
    }

    @Log(title = "现场检查-检查结论-点击重新编辑")
    @ApiOperation(value = "现场检查-检查结论-点击重新编辑")
    @GetMapping(value = "getJcjlCxbj")
    public HttpResult getJcjlCxbj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateXcjcCyjlCompleteState(sqsxid, false);
    }

    @Log(title = "综合评定-审查意见-完成状态")
    @ApiOperation(value = "综合评定-审查意见-完成状态(true: 完成; false: 未完成)")
    @GetMapping(value = "getZhpdScyjState")
    public HttpResult getZhpdScyjState(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.verifyZhpdScyjCompleteState(sqsxid);
    }

    @Log(title = "综合评定-审查意见-点击完成")
    @ApiOperation(value = "综合评定-审查意见-点击完成")
    @GetMapping(value = "getZhpdScyjDjwc")
    public HttpResult getZhpdScyjDjwc(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateZhpdScyjCompleteState(sqsxid, true);
    }

    @Log(title = "综合评定-审查意见-点击重新编辑")
    @ApiOperation(value = "综合评定-审查意见-点击重新编辑")
    @GetMapping(value = "getZhpdScyjCxbj")
    public HttpResult getZhpdScyjCxbj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateZhpdScyjCompleteState(sqsxid, false);
    }

    @Log(title = "综合评定-报告书-完成状态")
    @ApiOperation(value = "综合评定-报告书-完成状态(true: 完成; false: 未完成)")
    @GetMapping(value = "getZhpdBgsState")
    public HttpResult getZhpdBgsState(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.verifyZhpdBgsCompleteState(sqsxid);
    }

    @Log(title = "综合评定-报告书-点击完成")
    @ApiOperation(value = "综合评定-报告书-点击完成")
    @GetMapping(value = "getZhpdBgsDjwc")
    public HttpResult getZhpdBgsDjwc(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateZhpdBgsCompleteState(sqsxid, true);
    }

    @Log(title = "综合评定-报告书-点击重新编辑")
    @ApiOperation(value = "综合评定-报告书-点击重新编辑")
    @GetMapping(value = "getZhpdBgsCxbj")
    public HttpResult getZhpdBgsCxbj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return initWjjlService.updateZhpdBgsCompleteState(sqsxid, false);
    }

    @Log(title = "现场检查报告-点击签字")
    @ApiOperation(value = "现场检查报告-点击签字")
    @GetMapping(value = "xcjcbgQz")
    public HttpResult xcjcbgQz(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        Result result = initWjjlService.syncXcjcbgReplaceZxqm(sqsxid);
        if (!result.isSuccess()){
            log.error("现场检查报告-点击签字-异常: {}" + result.getMessage());
            return HttpResult.ok("已确认现场检查材料，现场检查报告请下载后签字上传到系统！");
        }

        return HttpResult.ok("操作成功");
    }

    @Log(title = "验证现场检查报告-是否已经签字（0: 未签字; 1: 已签字）")
    @ApiOperation(value = "验证现场检查报告-是否已经签字（0: 未签字; 1: 已签字）")
    @GetMapping(value = "yzjcbgQz")
    public HttpResult yzjcbgQz(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){
        return HttpResultVo.HttpResultGet(initWjjlService.verifySfQzByJcbg(sqsxid));
    }

}
